package site.marqstree.kotlin.library.good.ui.fragment.cartlist

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.frag_cart_list.*
import site.marqstree.kotlin.library.base.BR
import site.marqstree.kotlin.library.base.data.net.observer.RxObserver
import site.marqstree.kotlin.library.base.ui.fragment.BaseDataBindingFragment
import site.marqstree.kotlin.library.base.util.rxbus.RxBus
import site.marqstree.kotlin.library.good.R
import site.marqstree.kotlin.library.good.databinding.FragCartListBinding
import site.marqstree.kotlin.library.good.ui.bean.CartBean
import site.marqstree.kotlin.library.good.ui.bean.CartVOBean
import site.marqstree.kotlin.library.good.ui.bean.GoodVOBean
import site.marqstree.kotlin.library.good.ui.fragment.cartlist.event.EventAllSelectChange
import site.marqstree.kotlin.library.good.ui.fragment.cartlist.event.EventCartSumCountChange
import site.marqstree.kotlin.library.good.ui.fragment.cartlist.event.EventItemCountChange
import site.marqstree.kotlin.library.good.ui.fragment.cartlist.event.EventItemSelectChange
import site.marqstree.kotlin.library.good.ui.service.GoodService
import site.marqstree.kotlin.library.provider.router.ProviderRouter


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.good.ui.fragment.cartlist
 * 文件名: FragCartList
 * 创建者: marqstree
 * 创建时间: 2020/3/19 13:58
 * 描述: TODO
 */
@Route(path = ProviderRouter.Good.PATH_CART_LIST)
class FragCartList: BaseDataBindingFragment<FragCartListBinding, CartVOBean, CartFragPresenter>() {

    @Autowired
    @JvmField var mService: GoodService? = null
    override val BRDataId: Int = BR.cartVOBean
    override val mData: CartVOBean = CartVOBean()
    override val mLayoutId: Int = R.layout.frag_cart_list
    override val BRPresenterId: Int = BR.cartPresenter
    override val mPresenter: CartFragPresenter = CartFragPresenter()
    private val mAdapter = CartListAdapter()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initDataAndUI()
    }

    private fun initDataAndUI() {
        mService?.getCartList()
            ?.subscribe(object :RxObserver<CartBean>(){
                override fun onNext(data: CartBean) {
                    super.onNext(data)
                    //将网络接收数据转为VOBean
                    //VOBean通过DataBinding绑定UI
                    mData.goods = data.goods
                        .map{
                            GoodVOBean(it)
                        }
                        .toMutableList()
                    registerEventAllSelectChange()
                    registerEventItemCountChange()
                    registerEventItemSelect()
                    refreshSumPrice()
                    refreshCartSumCount()
                    initRecyclerView()
                    initPresenter()
                    initHeadBar()
                }
            })
    }

    private fun initHeadBar() {
        mHeaderBar.getRightView()
            .setOnClickListener {
                mData.mode.set(!mData.mode.get())
            }
    }

    fun initRecyclerView(){
        mGoodsRv.layoutManager = LinearLayoutManager(context)
        mGoodsRv.adapter = mAdapter
        mAdapter.setNewData(mData.goods)
    }

    //全选按钮事件
    fun registerEventAllSelectChange() {
        RxBus.observe<EventAllSelectChange>()
            .subscribe {
                refreshItemSelectByAllSelect()
                refreshSumPrice()
            }
    }

    //子项数量变更事件
    fun registerEventItemCountChange(){
        RxBus.observe<EventItemCountChange>()
            .subscribe{
                refreshCartSumCount()
            }
    }

    //刷新首页购物车总数角标
    private fun refreshCartSumCount() {
        val count = mData.goods
            .map {
                it.count.get()
            }
            .sum()
        refreshSumPrice()
        //向首页发送购物车总数变更事件
        RxBus.send(EventCartSumCountChange(count))
    }

    //子项选择状态
    fun registerEventItemSelect(){
        RxBus.observe<EventItemSelectChange>()
            .subscribe{
                refreshAllSelectByItem()
                refreshSumPrice()
            }
    }

    private fun refreshAllSelectByItem() {
        mData.selectAll.set(
            mData.goods
                .all{
                    it.selected.get()
                }
        )
    }

    fun initPresenter(){
        mPresenter.setAdapter(mAdapter)
    }

    fun refreshSumPrice(){
        mData.totalPrice.set(mData.goods
            .filter {
                it.selected.get()
            }
            .map {
                it.count.get()*it.price
            }
            .sum()
        )
    }

    fun refreshItemSelectByAllSelect() {
        val selectAll = mData.selectAll.get()
        //修改所有子项选中状态
        mData.goods
            .map {
                it.selected.set(selectAll)
            }
    }
}